package com.itheima.edu.info.manage.controller;

import com.itheima.edu.info.manage.dao.studentDao;
import com.itheima.edu.info.manage.domain.teacher;
import com.itheima.edu.info.manage.service.teacherService;

import java.util.Scanner;

public class teacherController {
    private Scanner sc = new Scanner(System.in);
    private teacherService teacherService = new teacherService();

    public void start() {

        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            teacherLoop:
            switch (choice) {
                case "1":
                    addteacher();
                    break;
                case "2":
                    deleteTeacherById();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统，再见！");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;

            }
        }

    }

    public void updateTeacher() {
        teacher[] teachers = teacherService.findAllStudent();
        if (teachers == null) {
            System.out.println("没有老师可以修改");
            return;
        }
        String id = inputTeacherId();

        teacher newTeacher = inputTeacherInfo(id);
        teacherService.updateTeacher(id, newTeacher);
        System.out.println("修改成功");
    }

    public void deleteTeacherById() {
        teacher[] teachers = teacherService.findAllStudent();
        if (teachers == null) {
            System.out.println("没有老师可以删除");
            return;
        }
        String id = inputTeacherId();
        teacherService.deleteTeacherById(id);
        System.out.println("删除成功");
    }

    public void findAllTeacher() {
        teacher[] teachers = teacherService.findAllStudent();
        if (teachers == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            teacher t = teachers[i];
            if (t != null) {
                System.out.println(t.getId() + "\t\t\t" + t.getName() + "\t " + t.getAge() + "\t   " + t.getBirthday());
            }
        }
    }

    public void addteacher() {
        String id;
        while (true) {
            System.out.println("请输入老师id：");
            id = sc.next();
            boolean exists = teacherService.isExist(id);
            if (exists) {
                System.out.println("您输入的id已被占用，请重新输入：");
            } else {
                break;
            }
        }
        teacher t = inputTeacherInfo(id);

        boolean result = teacherService.addTeacher(t);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入id");
            id = sc.next();
            boolean exists = teacherService.isExist(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入：");
            } else {
                break;
            }
        }
        return id;
    }

    public teacher inputTeacherInfo(String id) {
        System.out.println("请输入老师的姓名：");
        String name = sc.next();
        System.out.println("请输入老师的年龄：");
        String age = sc.next();
        System.out.println("请输入老师的生日：");
        String birthday = sc.next();
        teacher t = new teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBirthday(birthday);
        return t;
    }
}
